package com.hzit.content.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzit.common.result.Result;
import com.hzit.content.dto.ContentQuery;
import com.hzit.content.entity.Content;
import com.hzit.content.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/content")
public class ContentController {
    @Autowired
    private ContentService contentService;
    
    @PostMapping("/upload")
    @PreAuthorize("hasRole('USER')")
    public Result<String> uploadImage(@RequestParam("file") MultipartFile file) {
        String url = contentService.uploadImage(file);
        return Result.ok(url);
    }
    
    @GetMapping("/list")
    public Result<Page<Content>> list(ContentQuery query) {
        Page<Content> page = contentService.queryPage(query);
        return Result.ok(page);
    }
    
    @PostMapping("/save")
    @PreAuthorize("hasRole('USER')")
    public Result<Void> save(@RequestBody Content content) {
        contentService.save(content);
        return Result.ok();
    }
    
    @PutMapping("/update")
    @PreAuthorize("hasRole('USER')")
    public Result<Void> update(@RequestBody Content content) {
        contentService.updateById(content);
        return Result.ok();
    }
    
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('USER')")
    public Result<Void> delete(@PathVariable Long id) {
        contentService.removeById(id);
        return Result.ok();
    }
} 